package study.optional;

import study.optional.公共类.Car;
import study.optional.公共类.Insurance;
import study.optional.公共类.User;

import java.util.Optional;

/**
 * @Author 刘伟涛
 * @Date 2023/6/30 16:18
 **/
public class Demo01 {
    public static void main(String[] args) {
        /*传来的值*/
        User user = null;
        Car car = null;

        System.out.println(nullSafeFindBestInsurance(Optional.ofNullable(user),Optional.ofNullable(car)).map(Insurance::getName).orElse("这是空的"));

    }
    public static Insurance getBestInsurance(User user, Car car){
        return new Insurance("这是最好的保险");
    }
    public static Optional<Insurance> nullSafeFindBestInsurance(Optional<User> optUser,Optional<Car> optCar){
        return optUser.flatMap(u->optCar.map(c->getBestInsurance(u,c)));
    }
}
